package com.example.printservice.controller;

import com.example.printservice.constants.Constants;
import com.example.printservice.entity.AImage;
import com.example.printservice.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

@RestController
@Slf4j
public class ImgUrls {
    @RequestMapping("/getImages")
    public R getImageUrls(@RequestBody Map<String, Object> map) {
        String filePath = map.get("filePath").toString();
        System.out.println("filePath:" + filePath);
        File dir = new File(Paths.get(filePath).toString());
        File[] files = dir.listFiles();
        ArrayList<AImage> images = new ArrayList<>();
        assert files != null;
        for (File file : files) {
            String fileName = file.getName();
            images.add(new AImage(false, Integer.valueOf(fileName.substring(0, fileName.lastIndexOf("."))), Constants.SERVER_URL + "img/" + dir.getName() + "/" + fileName));
        }
        return R.ok().put("images", images);
    }
}
